﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using GroupChat2013.Common;
using System.Drawing;
using GroupChat2013.Entity;
using RTXCAPILib;
using RTXClient;
using System.Runtime.InteropServices;

namespace GroupChat2013.Common
{
    /// <summary>
    /// 帮助类
    /// </summary>
    public class Helper
    {
        /// <summary>
        /// 检测是否安装Chrome 
        /// </summary>
        /// <returns>Chrome路径</returns>
        public static string GetChromePath()
        {
            string str_ChromePath = string.Empty;
            string str_ChromeVersion = string.Empty;
            //Chrome_Version = string.Empty;
            //Chrome_Path = string.Empty;

            GetChromePath(out str_ChromePath, out str_ChromeVersion);
            if (!string.IsNullOrEmpty(str_ChromePath) && !string.IsNullOrEmpty(str_ChromeVersion))
            {
                return Path.Combine(str_ChromePath, "chrome.exe");
            }
            return string.Empty;
        }

        ///<summary> 
        /// 获取并返回当前安装的Chrome版本和安装路径 
        ///</summary> 
        ///<param name="str_ChromePath">Chrome的安装路径</param> 
        ///<param name="str_ChromeVersion">Chrome的安装版本</param> 
        private static void GetChromePath(out string str_ChromePath, out string str_ChromeVersion)
        {
            string str_InstallLocationResult = string.Empty;
            string str_VersionResult = string.Empty;
            string str_InstallLocation = "InstallLocation";
            string str_VersionID = "Version";
            object objResult = null;
            object objInstallLocation = null;
            Microsoft.Win32.RegistryValueKind regValueKind;//指定在注册表中存储值时所用的数据类型，或标识注册表中某个值的数据类型。
            Microsoft.Win32.RegistryKey regKey = null;//表示 Windows 注册表中的项级节点(注册表对象?) 
            Microsoft.Win32.RegistryKey regSubKey = null;
            try
            {
                regKey = Microsoft.Win32.Registry.LocalMachine;//读取HKEY_LOCAL_MACHINE项 
                regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome", false);//如果bool值为true则对打开的项进行读写操作,否则为只读打开

                objInstallLocation = regSubKey.GetValue(str_InstallLocation);
                regValueKind = regSubKey.GetValueKind(str_InstallLocation);
                if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
                {
                    str_InstallLocationResult = objInstallLocation.ToString();
                }

                objResult = regSubKey.GetValue(str_VersionID);
                regValueKind = regSubKey.GetValueKind(str_VersionID);
                if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
                {
                    str_VersionResult = objResult.ToString();
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (regKey != null)
                {
                    regKey.Close();
                    regKey = null;
                }

                if (regSubKey != null)
                {
                    regSubKey.Close();
                    regSubKey = null;
                }
            }
            str_ChromePath = str_InstallLocationResult;
            str_ChromeVersion = str_VersionResult;
        }

        /// <summary>
        /// 获取程序集路径
        /// </summary>
        /// <returns></returns>
        public static string GetAssemblyPath()
        {
            string _CodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;

            _CodeBase = _CodeBase.Substring(8, _CodeBase.Length - 8);    // 8是 file:// 的长度

            string[] arrSection = _CodeBase.Split(new char[] { '/' });

            string _FolderPath = "";
            for (int i = 0; i < arrSection.Length - 1; i++)
            {
                _FolderPath += arrSection[i] + "/";
            }

            return _FolderPath;
        }

        /// <summary>
        /// 判断窗体是否被遮挡
        /// </summary>
        /// <param name="hWnd">窗体句柄</param>
        /// <returns>返回窗体是否被完全遮挡</returns>
        public static bool WindowPall(IntPtr AHandle)
        {
            if (!WinApi.IsWindowVisible(AHandle)) return false; // 窗体不可见
            IntPtr vDC = WinApi.GetWindowDC(AHandle);
            try
            {
                Rectangle vRect = new Rectangle();
                WinApi.GetClipBox(vDC, ref vRect);
                return vRect.Width - vRect.Left <= 0 && vRect.Height - vRect.Top <= 0;
                // 特别说明：Rectangle.Width对应API中RECT.Right、Rectangle.Height为RECT.Bottom
            }
            finally
            {
                WinApi.ReleaseDC(AHandle, vDC);
            }
        }

        /// <summary>
        /// 窗体是否为最小化
        /// </summary>
        /// <param name="AHandle">句柄</param>
        /// <returns>是否最小化</returns>
        public static bool IsMinForm(IntPtr AHandle)
        {
            return WinApi.IsIconic(AHandle);
        }

        /// <summary>
        /// 获取RTX注册信息
        /// </summary>
        /// <returns>RTX注册信息</returns>
        public static RtxSignInfo GetRTXSign()
        {
            try
            {
                RTXClient.RTXAPIClass RTXCApi = new RTXAPIClass();  //创建客户端SDK主对象
                RTXCAPILib.RTXCRoot objKerRoot = (RTXCRoot)RTXCApi.GetObject("KernalRoot"); //通过主对象获取CRoot对象

                RtxSignInfo signInfo = new RtxSignInfo();
                signInfo.Account = objKerRoot.Account;  //通过CRoot对象获取本机登录帐号

                RTXCAPILib.RTXCData objCData = objKerRoot.Sign; //通过CRoot对象向服务器获取签名
                signInfo.Sign = objCData.GetString("Sign"); //由于服务器返回的是一个CDATA对象，因此通过GetString把签名字符串提取出来

                LogHelper.LogInfo(string.Format("GetRTXSign() 获取到的登录帐号：{0} 签名：{1} ", signInfo.Account, signInfo.Sign));
                return signInfo;
            }
            catch (COMException ex)
            {
                LogHelper.LogError(string.Format("GetRTXSign Error Message:{0}/r/nStackTrace{1}", ex.Message, ex.StackTrace));
                return null;
            }
        }

        /// <summary>
        /// 获取程序域根路径
        /// </summary>
        /// <returns>路径</returns>
        public static string GetBaseDir()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }

        /// <summary>
        /// 拼接路径
        /// </summary>
        /// <param name="pathPath">要拼接的路径</param>
        /// <returns>拼接好的路径</returns>
        public static string CombinePath(string pathPath)
        {
            return Path.Combine(GetBaseDir(), pathPath);
        }

        /// <summary>
        /// 拼接路径
        /// </summary>
        /// <param name="url">路径</param>
        /// <param name="param">要拼接的参数</param>
        /// <returns>完整路径</returns>
        public static string AddUrlParams(string url, string param)
        {
            if (url.IndexOf("?") > 0)
                return url + "&" + param;
            else
                return url + "?" + param;
        }
    }
}
